package com.example.mylibrarybase.yumengmeng13.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.mylibrarybase.R;
import com.example.mylibrarybase.yumengmeng12.base.baseRecyclerview;
import com.example.mylibrarybase.yumengmeng13.bean.item_j;

import java.util.List;

/**
 * Created by 35876 于萌萌
 * 创建日期: 17:18 . 2016年10月21日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public class cheboxRecyclerview extends baseRecyclerview<item_j> {
   public Callback callback;
    public cheboxRecyclerview(Context context, List<item_j> mTs,Callback callback) {
        super(context, mTs);
        this.callback =callback;
    }

    @Override
    protected itemView initonCreateViewHolder(ViewGroup parent, int viewType) {
            //必须 脱离父子关系  写成null 要不然 You must call removeView() on the child's parent
        my_itemView my_itemView = new my_itemView(mLayoutInflater.inflate(R.layout.item_view_13, null));
        my_itemView.initview();
        return my_itemView;
    }

    @Override
    protected void initonBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof my_itemView){
                my_itemView holder1 = (my_itemView) holder;
                item_j item_j = mTs.get(position);
                holder1.initdata(item_j);
            }
    }

    public class my_itemView extends itemView{

        private TextView mItem_view_t;
        private CheckBox item_view_c;

        public my_itemView(View itemView) {
            super(itemView);
        }


        public void initview() {
            mItem_view_t = (TextView) itemView.findViewById(R.id.item_view_t);
            item_view_c = (CheckBox) itemView.findViewById(R.id.item_view_c);
        }

        public void initdata(final item_j item_j) {
            mItem_view_t.setText(item_j.t);
           if (item_j.tag ==0){
               item_view_c.setChecked(false);
           }else{
               item_view_c.setChecked(true);
           }
            item_view_c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                    item_j item_j = mTs.get(getLayoutPosition());
                    if (b==true){
                        item_j.tag = 1;
                    }else{
                        item_j.tag = 0;
                    }

                    callback.oncheboxclick(getLayoutPosition(),  item_j.tag );
                }
            });

        }
    }


    public interface  Callback{
        void oncheboxclick(int pos, int boolentag);
    }

    public  void  setCallback(Callback callback){
        this.callback =callback;
    }
}
